//app.js
const utils = require('./utils/util');
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now());
    wx.setStorageSync('logs', logs);

    var studiedDays = 1;
    var studiedDaystmp = wx.getStorageSync(utils.studiedDays);
    if(studiedDaystmp === null || studiedDaystmp === undefined || isNaN(studiedDaystmp)) {
        studiedDays = Number.parseInt(1);
    } else {
        studiedDays = Number.parseInt(studiedDaystmp) + Number.parseInt(1);
    }
    wx.setStorageSync(utils.studiedDays, Number.parseInt(studiedDays));
    // 设置目标类型 4-四级 6-六级
    wx.setStorageSync(utils.wordType, {id: 4, index: 0, course_num: 226, class_id: 45090,  name: '四级必备'});
    // 设置计划学习天数
    wx.setStorageSync(utils.planStudyDays, 365);
    // 设置每天计划学习单词个数
    wx.setStorageSync(utils.planStudyWordsOfDay, 3);
    // 设置每天计划复习单词个数
    wx.setStorageSync(utils.planReviewWordsOfDay, 0);
  },
  getUserInfo:function(cb){
    var that = this;
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo;
              typeof cb == "function" && cb(that.globalData.userInfo);
            }
          })
        }
      });
    }       
  },
  globalData:{
    userInfo:null
  }
})
